/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.chain.shell;

import tal.drivechain.chain.ChainNode;
import tal.drivechain.module.xmlconfig.PropertyInfo;
import tal.drivechain.parsing.Tokenizer;
import tal.drivechain.variable.SetProperty;

/**
 *
 * @author eisental
 */
public class PropertyObject extends ShellObject {

    public PropertyObject(DriveShell shell) { super("property", "properties", "prop", shell); }

    @Override
    public boolean isActionSupported(Action action) {
        return (action==Action.set || action==Action.inspect);
    }

    @Override
    public void set(String command) throws IllegalArgumentException {
        //set prop [<flag>] <node name> <property[: value]>;
        String[] words = new Tokenizer(command, ' ').getTokens();
        String nodeName, property;
        SetProperty.Flag flag;

        if (words.length==3) {
            flag = SetProperty.Flag.parseFlag(words[0]);
            nodeName = words[1].trim();
            property = words[2].trim();
        } else if (words.length==2) {
            flag = SetProperty.Flag.REPLACE;
            nodeName = words[0].trim();
            property = words[1].trim();
        } else throw new IllegalArgumentException("Invalid syntax (should be: set prop [flag] <instance name> <property[: value]> )");

        ChainNode node = shell.getActiveChain().getNodeByPath(nodeName);
        if (node==null)
            throw new IllegalArgumentException("Unknown node: " + nodeName);

        if (!(property.charAt(0)=='{') || !(Tokenizer.balanced(property, 0, '{', '}')==property.length()-1))
            throw new IllegalArgumentException("Property must be surrounded by curly brackets ({})");
        else property = property.substring(1, property.length()-1).trim();

        SetProperty setProp = new SetProperty(node.getNodeClassName(), property, flag);

        setProp.invokeSetProperty(node);
        out.println("Property set: " + setProp.toString());
    }

    @Override
    public void inspect(String command) throws IllegalArgumentException {
        //inspect prop <instance name> <property>
        String words[] = new Tokenizer(command, ' ').getTokens();
        if (words.length!=2) throw new IllegalArgumentException("Invalid syntax (should be: inspect prop <instance name> <property name> )");

        String nodeName = words[0];
        String property = words[1];

        ChainNode node = shell.getActiveChain().getChildNodes().get(nodeName);
        if (node==null)
            throw new IllegalArgumentException("Unknown node name: " + nodeName);
        PropertyInfo prop = null;
        for (PropertyInfo p : node.getNodeInfo().getProperties()) {
            if (p.getName().equals(property))
                prop = p;
        }
        if (prop==null)
            throw new IllegalArgumentException("Unknown property name: " + property);
        
        out.println(InfoPrinter.printProperty(node, prop, node.getProperty(property)));

    }

    @Override
    public void make(String command) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void remove(String command) throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }

    @Override
    public void list() throws IllegalArgumentException {
        throw new UnsupportedOperationException("Not supported.");
    }


}
